#include <iostream>
using namespace std;

bool Find(int* matrix, int rows, int columns, int number)
{
	if(matrix == NULL) return false;
	if(rows <= 0 || columns <= 0) return false;

	int row = 0, column = columns - 1;
	while(row < rows && column >= 0)
	{
		int n = *(matrix + row * columns + column);
		if(n == number)
		{
			return true;
		}
		else if(n > number)
		{
			column--;
		}
		else  row++;
	}
	return false;
}

int main()
{
	int arr[][4] = {
		{1, 2, 8, 9},
		{2, 4, 9, 12},
		{4, 7, 10, 13},
		{6, 8, 11, 15}
	};

	// 能找到的测试
	bool result = Find((int*)arr, 4, 4, 10);
	cout << result << endl;
	result = Find((int*)arr, 4, 4, 1);
	cout << result << endl;
	result = Find((int*)arr, 4, 4, 9);
	cout << result << endl;
	result = Find((int*)arr, 4, 4, 6);
	cout << result << endl;
	result = Find((int*)arr, 4, 4, 15);
	cout << result << endl;

	// 找不到的测试
	result = Find((int*)arr, 4, 4, 0);
	cout << result << endl;
	result = Find((int*)arr, 4, 4, 16);
	cout << result << endl;
	
	// 鲁棒性测试
	result = Find(NULL, 4, 4, 16);
	cout << result << endl;
	result = Find((int*)arr, 0, 4, 16);
	cout << result << endl;
	result = Find((int*)arr, 4, 0, 16);
	cout << result << endl;
	result = Find((int*)arr, 0, 0, 16);
	cout << result << endl;
	
	return 0;
}
